package com.soup.view;

import com.soup.dao.BookDao;
import com.soup.domain.Book;
import com.soup.view.component.Context;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class TopFrame extends Context {

    private JTextField one = new JTextField("无");
    private JTextField two = new JTextField("无");
    private JTextField three = new JTextField("无");
    private JTextField four = new JTextField("无");
    private JTextField five = new JTextField("无");
    private JTextField sex = new JTextField("无");
    private JTextField seven = new JTextField("无");
    private JTextField eight = new JTextField("无");
    private JTextField night = new JTextField("无");

    private BookDao dao = new BookDao();
    ArrayList<JTextField> set = new ArrayList<JTextField>();

    public TopFrame(int width, int height) {
        super(width, height);
        one.setBounds(60, 0, width-120, 40);
        two.setBounds(60, one.getHeight()+one.getLocation().y+10, width-120, 40);
        three.setBounds(60, two.getHeight()+two.getLocation().y+10, width-120, 40);
        four.setBounds(60, three.getHeight()+three.getLocation().y+10, width-120, 40);
        five.setBounds(60, four.getHeight()+four.getLocation().y+10, width-120, 40);
        sex.setBounds(60, five.getHeight()+five.getLocation().y+10, width-120, 40);
        seven.setBounds(60, sex.getHeight()+sex.getLocation().y+10, width-120, 40);
        eight.setBounds(60, seven.getHeight()+seven.getLocation().y+10, width-120, 40);
        night.setBounds(60, eight.getHeight()+eight.getLocation().y+10, width-120, 40);
        this.add(one,"第一",100);one.setEditable(false);
        this.add(two, "第二", 100);two.setEditable(false);
        this.add(three, "第三", 100);three.setEditable(false);
        this.add(four, "第四", 100);four.setEditable(false);
        this.add(five, "第五", 100);five.setEditable(false);
        this.add(sex, "第六", 100);sex.setEditable(false);
        this.add(seven, "第七", 100);seven.setEditable(false);
        this.add(eight, "第八", 100);eight.setEditable(false);
        this.add(night, "第九", 100);night.setEditable(false);
        set.add(one);set.add(two);set.add(three);set.add(four);set.add(five);set.add(sex);
        set.add(seven);set.add(eight);set.add(night);

        List<Book> byName = dao.findByName("%");
        byName.sort((o1, o2) -> o2.getHot() - o1.getHot());
        System.out.println(byName);
        if (byName.size() > 9){
            for (int i = 0; i < 9; i++) {
                set.get(i).setText(byName.get(i).getName());
            }
        }else {
            for (int i = 0; i < byName.size(); i++) {
                set.get(i).setText(byName.get(i).getName()+"--热度"+byName.get(i).getHot());
            }
        }
    }
}
